<?php

class HistoryTable extends Doctrine_Table
{
    public function getLastLog($datetime, $playerid, $raidid)
    {
        $q = $this->createQuery("history")
                ->where("dt=?", $datetime)
                ->andWhere("playerid=?", $playerid)
                ->andWhere("raidid=?", $raidid)
                ->addOrderBy("savedate desc");
        return $q->execute();
    }

    public function getLastLogHistory($history)
    {
        return $this->getLastLog($history->getDt(), $history->getPlayerid(), $history->getRaidid());
    }

    public function getSumValue($date, $raidid, $playerid, $aaction=array())
    {
        $query = Doctrine::getTable("History")
                ->createQuery()
                ->select("value")
                ->where("dt=?", $date)
                ->andWhere("raidid=?", $raidid)
                ->andWhere("playerid=?", $playerid)
                ->limit(1);
        if (count($aaction) > 0)
            $query->andWhereIn("aaction", $aaction);
        return $query->execute();
    }

    public function getHistory($date, $raidid, $playerid, $aaction=array())
    {
        return $this->getSumValue($date, $raidid, $playerid, $aaction);
    }

    public function retrieveHistoryForPlayer(Doctrine_Query $q)
    {
        $q->select("savedate, actiondate, dt, raidid, value, ttype, waitlistflag, itemid, zoneid, reasonid, comment, created_by, updated_by");
    }

    public function getBackendList(Doctrine_Query $q)
    {
        $rootAlias = $q->getRootAlias();
        $q->leftJoin($rootAlias.".AAction aa")
                ->leftJoin($rootAlias.".TType t")
                ->leftJoin($rootAlias.".Player p")
                ->leftJoin($rootAlias.".Raid raid")
                ->leftJoin($rootAlias.".Zone z")
                ->leftJoin($rootAlias.".Reason reason");
        return $q;
    }

    public function getScheduledRaid($dt)
    {
        $schedule = array();
        $schedule[0]['begin'] = csSettings::get("sunday_begin");
        $schedule[0]['end'] =   csSettings::get("sunday_end");
        $schedule[1]['begin'] = csSettings::get("monday_begin");
        $schedule[1]['end'] =   csSettings::get("monday_end");
        $schedule[2]['begin'] = csSettings::get("tuesday_begin");
        $schedule[2]['end'] =   csSettings::get("tuesday_end");
        $schedule[3]['begin'] = csSettings::get("wednesday_begin");
        $schedule[3]['end'] =   csSettings::get("wednesday_end");
        $schedule[4]['begin'] = csSettings::get("thursday_begin");
        $schedule[4]['end'] =   csSettings::get("thursday_end");
        $schedule[5]['begin'] = csSettings::get("friday_begin");
        $schedule[5]['end'] =   csSettings::get("friday_end");
        $schedule[6]['begin'] = csSettings::get("saturday_begin");
        $schedule[6]['end'] =   csSettings::get("saturday_end");

        $return = null;
        $date = null;
        $time = null;
        if (is_array($dt))
        {
            $date = $dt['year'].'-'.$dt['month'].'-'.$dt['day'];
        }
        else
        {
            list($date, $time) = explode(" ", $dt);
        }
        $datets = strtotime($date." GMT");
        $ts = strtotime($dt." GMT");
        for ($d = -1; $d < 1; $d++)
        {
            $str = "+1 days";
            if ($d == -1)
                $str = "-1 days";
            $dts = $datets;

            if ($d != 0)
                $dts = strtotime($str, $dts);
            for ($i = 0; $i < count($schedule); $i++)
            {
                if (is_string($schedule[$i]['begin']))
                {
                    list($hour, $minute) = explode(":", $schedule[$i]['begin']);
                    $begin = $datets + intval($hour)*60*60 + intval($minute)*60;
                    list($hour, $minute) = explode(":", $schedule[$i]['end']);
                    $end = $datets + intval($hour)*60*60 + intval($minute)*60;
                    if ($end <= $begin)
                        $end =   strtotime("+1 day", $end);
                    if ($begin <= $ts && $end >= $ts)
                    {
                        $return = $date;
                        break;
                        break;
                    }
                }
            }
        }
        return $return;
    }

}